<template>
  <div class="customerManage">
    <page-header :title="'客户管理'"></page-header>
    <div class="search_area">
      <el-row>
        <el-form :form="queryForm" label-width="1rem">
          <el-col :span="8">
            <el-form-item label="公司名称">
              <el-input type="text" v-model="queryForm.companyName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="所有者">
              <el-select v-model="queryForm.owner" clearable placeholder="请选择" style="width:100%">
                <el-option v-for="item in ownerList" :label="item.userName" :value="item.uuid" :key="item.uuid"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-form>
        <el-col :span="24">
          <div class="btn_area">
            <el-button class="confrim" @click="queryList">查询</el-button>
            <el-button class="cancle" @click="reset">重置</el-button>
            <el-button class="confrim" @click="add">新增</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <table-list :fields="fields" :list="list" :pagination="pagination" :total="total" @sizeChange="handleSizeChange" @currentChange="handleCurrentChange" @handle="onHandle" :loading="loading"></table-list>
    <el-dialog :title="code == '1' ? '新增客户': '编辑客户'" :visible.sync="visible" @close="close" width="65%">
      <el-row>
        <el-form :form="customForm" label-width="1.5rem">
          <el-col :span="11">
            <el-form-item label="公司名称" required>
              <el-input v-model="customForm.companyName" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="公司地址" required>
              <el-input v-model="customForm.address" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="公司座机" required>
              <el-input v-model="customForm.tel" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="所属市场活动" required>
              <el-select
                v-model="customForm.activityIds"
                multiple
                collapse-tags
                placeholder="请选择"
                style="width: 100%">
                <el-option
                  v-for="item in market"
                  :key="item.uuid"
                  :label="item.activityName + '-' + item.ownerName"
                  :value="item.uuid">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="客户所有者" required>
              <el-select v-model="customForm.owner" clearable placeholder="请选择" style="width:100%">
                <el-option v-for="item in ownerList" :label="item.userName" :value="item.uuid" :key="item.uuid"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="联系纪要" required>
              <el-input type="textarea" v-model="customForm.contactNotes" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="描述">
              <el-input type="textarea" v-model="customForm.described" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="创建人">
               <el-input disabled v-model="customForm.createByName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="创建时间">
              <el-input disabled v-model="customForm.createTime"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="更新人">
              <el-input disabled v-model="customForm.updateByName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="更新时间">
              <el-input disabled v-model="customForm.updateTime"></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <el-divider v-if="code !== '1'"></el-divider>
      <div v-if="code !== '1'">
        <div style="margin-bottom: 0.2rem;">
          <span>联系人列表</span>
        </div>
        <table-list :fields="fields1" :list="contact" :pagination="{currentChange: 1,pageSize:100}" :total="total1" @handle="onHandle2" :page="false"></table-list>
      </div>
      <div v-if="code !== '1'">
        <div style="margin-bottom: 0.2rem;">
          <span>交易列表</span>
        </div>
        <table-list :fields="fields2" :list="tran" @handle="onHandle2" :page="false" :pagination="{currentChange: 1,pageSize:100}"></table-list>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="cancle" @click="visible = false">取 消</el-button>
        <el-button class="confrim" @click="confrim">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog :title="code1 == '1' ? '新增联系人': '编辑联系人'" :visible.sync="visible1" @close="close1" width="65%">
      <el-row>
        <el-form :form="emptyContact" label-width="1.5rem">
          <el-col :span="11">
            <el-form-item label="联系人名称" required>
              <el-input v-model="emptyContact.contactName" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="客户职位">
              <el-input v-model="emptyContact.position" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="客户联系方式" required>
              <el-input v-model="emptyContact.phone" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="客户邮箱">
              <el-input v-model="emptyContact.mail" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="客户生日">
              <el-date-picker
                v-model="emptyContact.birthday"
                style="width: 100%"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="所有者" required>
              <el-select v-model="emptyContact.owner" clearable placeholder="请选择" style="width:100%">
                <el-option v-for="item in ownerList" :label="item.userName" :value="item.uuid" :key="item.uuid"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="描述">
              <el-input type="textarea" v-model="emptyContact.described" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button class="cancle" @click="visible1 = false">取 消</el-button>
        <el-button class="confrim" @click="confrim1">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import tableList from '@/components/table.vue'
import pageHeader from '@/components/pageHeader.vue'
export default {
  name: 'customerManagement',
  components: {
    tableList,
    pageHeader
  },
  data() {
    return {
      code: '',
      code1: '',
      loading: false,
      queryForm: {
        companyName: '',
        owner: ''
      },
      list: [],
      fields: [
        {
          prop: 'rowNum',
          label: '序号',
          align: 'center'
        },
        {
          prop: 'companyName',
          label: '公司名称',
          align: 'center'
        },
        {
          prop: 'tel',
          label: '座机',
          align: 'center'
        },
        {
          prop: 'ownerName',
          label: '所有者',
          align: 'center'
        },
        {
          prop: 'createByName',
          label: '创建人',
          align: 'center'
        },
        {
          prop: 'createTime',
          label: '创建时间',
          align: 'center'
        },
        {
          prop: 'oper',
          label: '操作',
          align: 'center',
          width: '200',
          btns: [
            {type: 'update',desc:'编辑'},
            {type: 'contact',desc:'添加联系人'},
          ]
        }
      ],
      fields1: [
        {
          prop: 'contactName',
          label: '联系人名称',
          align: 'center'
        },
        {
          prop: 'position',
          label: '职位',
          align: 'center'
        },
        {
          prop: 'mail',
          label: '邮箱',
          align: 'center'
        },
        {
          prop: 'phone',
          label: '联系方式',
          align: 'center'
        },
        {
          prop: 'oper',
          label: '操作',
          align: 'center',
          btns: [
            {type: 'update',desc:'编辑'}
          ]
        }
      ],
      fields2: [
        {
          prop: 'tranName',
          label: '交易名称',
          align: 'center'
        },
        {
          prop: 'customerName',
          label: '客户名称',
          align: 'center'
        },
        {
          prop: 'contact',
          label: '联系人',
          align: 'center'
        },
        {
          prop: 'stageName',
          label: '阶段名称',
          align: 'center'
        },
        {
          prop: 'ownerName',
          label: '所有者',
          align: 'center'
        },
        {
          prop: 'createByName',
          label: '创建人',
          align: 'center'
        }
      ],
      pagination: {
        currentPage: 1,
        pageSize: 10
      },
      customForm: {
        "activityIds": [],
        "address": "",
        "adminId": "",
        "companyName": "",
        "contactNotes": "",
        "createBy": "",
        "createByName": "",
        "createTime": "",
        "described": "",
        "owner": "",
        "tel": "",
        "updateBy": "",
        "updateByName": "",
        "updateTime": "",
      },
      emptyContact: {
        adminId: "",
        birthday: "",
        contactName: "",
        createBy: "",
        customerId: "",
        described: "",
        mail: "",
        owner: "",
        phone: "",
        position: "",
        updateBy: "",
        uuid: ""
      },
      contact: [], // 联系人列表
      tran: [], // 交易列表
      total: 0,
      total1: 0,
      visible: false,
      visible1: false,
      userData: {},
      market: [],
      ownerList: [],
      customerId: ''
    }
  },
  methods: {
    onHandle(data) {
      let {row,type} = data;
      this.customerId = row.uuid;
      if (type == 'update') {
        this.visible = true;
        this.code = '2';
        this.$post(this.$api.custom.detail,this.reqData(row.uuid)).then(res => {
          if (res.data.code == 200) {
            let data = res.data.data;
            let activityIds = data.activityList.map(item => item.uuid)
            console.log(activityIds);
            this.customForm =  {
              "activityIds": activityIds,
              "address": data.address ? data.address : "",
              "adminId": this.userData.uuid,
              "companyName": data.companyName ? data.companyName : "",
              "contactNotes": data.contactNotes ? data.contactNotes : "",
              "createByName": data.createByName ? data.createByName : "",
              "createTime": data.createTime ? data.createTime : "",
              "described": data.described ? data.described : "",
              "owner": data.owner ? data.owner : "",
              "tel": data.tel ? data.tel : "",
              "updateByName": data.updateByName ? data.updateByName : "",
              "updateTime": data.updateTime ? data.updateTime : "",
              "uuid": data.uuid,
            }
            this.contact = data.contactList
            this.tran = data.tranList
            this.total1 = this.contact.length;
            console.log(this.customForm);
          } else {
            this.$message.error(res.data.message)
          }
        })
      } else {
        this.customerId = row.uuid;
        this.addContact()
      }
    },
    onHandle2(data) {
      let {row,type} = data;
      this.visible1 = true;
      this.code1= '2';
      this.$post(this.$api.contact.info,this.reqData(row.uuid)).then(res => {
        if (res.data.code == 200) {
          let data = res.data.data;
          this.emptyContact = data;
          this.emptyContact.adminId = this.userData.uuid;
          console.log(this.customForm);
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    add() {
      this.code = '1'
      this.visible = true;
      this.customForm.adminId = this.userData.uuid;
      this.customForm.createBy = this.userData.uuid;
      this.customForm.createTime = this.$time();
    },
    addContact() {
      this.code1 = '1'
      this.visible1 = true;
      this.emptyContact.adminId = this.userData.uuid;
    },
    confrim() {
      console.log(this.customForm);
      let url = this.code == '1' ? this.$api.custom.create : this.$api.custom.update
      this.$post(url,this.reqData(this.customForm)).then(res => {
        if (res.data.code === 200) {
          this.$message.success(res.data.message);
          this.visible = false;
          this.queryList()
        } else {
          this.$message.error(res.data.message);
        }
      })
    },
    close() {
      this.contact = [];
      this.tran = [];
      this.total1 = 0;
      this.customForm = {
        "activityIds": [],
        "address": "",
        "adminId": "",
        "companyName": "",
        "contactNotes": "",
        "createBy": "",
        "createByName": "",
        "createTime": "",
        "described": "",
        "owner": "",
        "tel": "",
        "updateBy": "",
        "updateByName": "",
        "updateTime": "",
      }
    },
    close1() {
      this.emptyContact = {
        adminId: "",
        birthday: "",
        contactName: "",
        createBy: "",
        customerId: "",
        described: "",
        mail: "",
        owner: "",
        phone: "",
        position: "",
        updateBy: "",
        uuid: ""
      }
    },
    confrim1() {
      this.emptyContact.customerId = this.customerId;
      let url = this.code1 == '1' ? this.$api.contact.create : this.$api.contact.update
      this.$post(url,this.reqData(this.emptyContact)).then(res => {
        if (res.data.code === 200) {
          this.$message.success(res.data.message);
          this.$post(this.$api.custom.detail,this.reqData(this.customerId)).then(res => {
          if (res.data.code == 200) {
            let data = res.data.data;
            this.contact = data.contactList
          }
        })
          this.visible1 = false;
        } else {
          this.$message.error(res.data.message);
        }
      })
    },
    queryList() {
      this.loading = true
      this.$post(this.$api.custom.list,this.reqData(this.queryForm,this.pagination.currentPage,this.pagination.pageSize)).then(res => {
        this.loading = false
        if (res.data.code === 200) {
          let data = res.data.data
          this.list = data.list;
          this.list.forEach((item,index) => {
            item.rowNum = (this.pagination.currentPage - 1) * this.pagination.pageSize + index + 1;
          })
          this.total = data.total;
        } else {
          this.$message.error(res.data.message)
        }
      }).catch(e => {
        this.loading = false
      })
    },
    reset() {
      this.queryForm.companyName = "";
      this.queryForm.owner = "";
    },
    handleSizeChange(val) {
      console.log(val);
      this.pagination.pageSize = val;
      this.queryList()
    },
    handleCurrentChange(val) {
      console.log(val);
      this.pagination.currentPage = val;
      this.queryList()
    },
    marketList() {
      let url = this.$api.market.list;
      this.$post(url,this.reqData({
        activityName: '',
        owner: '',
        startDate: '',
        endDate: '',
        leadId: ''
      },1,999)).then(res => {
        if (res.data.code == 200) {
          let data = res.data.data
          this.market = data.list;
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    queryUserList() {
      let url = this.$api.user.list;
      this.$post(url).then(res => {
        if (res.data.code == 200) {
          this.ownerList = res.data.data;
        }
      })
    },
  },
  mounted() {
    this.userData = JSON.parse(sessionStorage.getItem('userData'))
    this.marketList();
    this.queryUserList();
    this.queryList()
  }
}
</script>
<style lang="less" scoped>
.customerManage {
  background: #fff;
  overflow: hidden;
  padding-bottom: 0.15rem;
}
</style>